package kr.hkit.summart_java.serviceUI;

import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import kr.hkit.myframework.uicomp.InputPanel;
import kr.hkit.myframework.uicomp.InputPanel2;
import kr.hkit.summart_java.dao.DaoInterface;
import kr.hkit.summart_java.erp.dto.Employee;

public class PanelEmployeeUI extends JPanel implements ActionListener {
	private JPanel panel_Ctr;
	private JPanel panel_Btm;
	private InputPanel pEmpNo;
	private InputPanel pEmpName;
	private InputPanel pEmptitle;
	private InputPanel pEmpmanger;
	private InputPanel pEmpsalary;
	private InputPanel pEdeptNo;

	public static JButton ebtnadd;
	public static JButton ebtndel;
	public static JButton ebtnupd;
	public static JButton ebtnsear;
	private DaoInterface<Employee> dao2;
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private InputPanel2 pEdeptNo2;


	/**
	 * Create the panel.
	 */
	public PanelEmployeeUI() {

		initialize();
	}
	public void initialize() {
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		panel_Ctr = new JPanel();
		add(panel_Ctr);
		panel_Ctr.setLayout(new BoxLayout(panel_Ctr, BoxLayout.Y_AXIS));
		
		pEmpNo = new InputPanel("사원번호");
		panel_Ctr.add(pEmpNo);
		
		pEmpName = new InputPanel("사원명");
		panel_Ctr.add(pEmpName);
		pEmpName.setLayout(new GridLayout(1, 0, 0, 0));
		
		pEmptitle = new InputPanel("직급번호");
		panel_Ctr.add(pEmptitle);
	
		pEmpmanger = new InputPanel("직속상관");
		panel_Ctr.add(pEmpmanger);
		
		pEmpsalary = new InputPanel("급여");
		panel_Ctr.add(pEmpsalary);
		
//		pEdeptNo = new InputPanel("부서명");
//		panel_Ctr.add(pEdeptNo);
		
		label = new JLabel("");
		panel_Ctr.add(label);
		
		pEdeptNo2 = new InputPanel2("부서명2");
		panel_Ctr.add(pEdeptNo2);
		pEdeptNo2.setLayout(new GridLayout(0, 2, 0, 0));
		
		
		panel_Btm = new JPanel();
		add(panel_Btm);
		
		ebtnadd = new JButton("");
		ebtnadd.addActionListener(this);
		panel_Btm.add(ebtnadd);

	}

	public void actionPerformedadd(ActionEvent e) {
		int eno = Integer.parseInt(pEmpNo.getTf().trim());
		String empname = pEmpName.getTf();
		int emptitle = Integer.parseInt(pEmptitle.getTf().trim());
		int empman = Integer.parseInt(pEmpmanger.getTf().trim());
		int empsal = Integer.parseInt(pEmpsalary.getTf().trim());
		int edno = Integer.parseInt(pEdeptNo.getTf().trim());

		Employee emp = new Employee(eno, empname, emptitle, empman, empsal, edno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao2.insertItem(emp);
			JOptionPane.showMessageDialog(this, "입력되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "입력실패.");
		}
	}
	public void actionPerformeddel(ActionEvent e) {
		int eno = Integer.parseInt(pEmpNo.getTf().trim());
		String empname = pEmpName.getTf();
		int emptitle = Integer.parseInt(pEmptitle.getTf().trim());
		int empman = Integer.parseInt(pEmpmanger.getTf().trim());
		int empsal = Integer.parseInt(pEmpsalary.getTf().trim());
		int edno = Integer.parseInt(pEdeptNo.getTf().trim());

		Employee emp = new Employee(eno, empname, emptitle, empman, empsal, edno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao2.deleteItemByNo(emp);
			JOptionPane.showMessageDialog(this, "삭제되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "실패.");
		}
	}
	public void actionPerformedupd(ActionEvent e) {
		int eno = Integer.parseInt(pEmpNo.getTf().trim());
		String empname = pEmpName.getTf();
		int emptitle = Integer.parseInt(pEmptitle.getTf().trim());
		int empman = Integer.parseInt(pEmpmanger.getTf().trim());
		int empsal = Integer.parseInt(pEmpsalary.getTf().trim());
		int edno = Integer.parseInt(pEdeptNo.getTf().trim());

		Employee emp = new Employee(eno, empname, emptitle, empman, empsal, edno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao2.updateItemByNo(emp);
			JOptionPane.showMessageDialog(this, "수정되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "실패.");
		}
	}
	public void actionPerformedbtnsear(ActionEvent e) {
		Employee item = null;
		int eno = Integer.parseInt(pEmpNo.getTf().trim());
		String empname = pEmpName.getTf();
		int emptitle = Integer.parseInt(pEmptitle.getTf().trim());
		int empman = Integer.parseInt(pEmpmanger.getTf().trim());
		int empsal = Integer.parseInt(pEmpsalary.getTf().trim());
		int edno = Integer.parseInt(pEdeptNo.getTf().trim());
		Employee emp = new Employee(eno, empname, emptitle, empman, empsal, edno);
		JOptionPane.showMessageDialog(this, emp);
		try {
			dao2.searchItem(emp);
			item = dao2.searchItem(emp);
			label.setText(item+"");
			JOptionPane.showMessageDialog(this, "검색되었습니다.");
		} catch (SQLException e1) {
			JOptionPane.showMessageDialog(this, "실패.");
		}
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		if(ebtnadd.getText()=="추가"){
			actionPerformedadd(e);
		}else if(ebtnadd.getText()=="삭제"){
			actionPerformeddel(e);
		}else if(ebtnadd.getText()=="수정"){
			actionPerformedupd(e);
		}else{
			actionPerformedbtnsear(e);
		}
		
	}

	public void setDao(DaoInterface<Employee> dao) {
		this.dao2 = dao;
	}
}
